/*
************
**panel.js**
************

panel.html
1. data_structure.js
2. panel.js
3. element.js

- 全局变量
- 回调函数
*/


/*
=======================================
回调函数类
=======================================
*/
class CallBack{
    constructor(){

    }
}

class CallBackSimpleList extends CallBack{
    constructor(){
        super();
        this.obj_DataSet = mc_GetData.simple_list.data_set;
        this.obj_DataBindingSet = mc_GetData.simple_list.data_binding_set;
        this.obj_WarningConfirm = new WarningConfirmBar();
    }

    // ===============
    // 数据组
    // ================

    cb_change_data_set_target_value_xpath(msg){
        console.log("==cb_change_data_set_target_value_xpath==");
        if (msg.is_success === -1){
            console.log("faile");
            // web_info_object.display_warning("获取的数据不在指定的结构范围之内");
            // utility_for_panel.animateScroll($("#div_warning_collapse")[0], 10);
            this.obj_WarningConfirm.display_warning("获取的数据不在指定的结构范围之内");
            return false;
        }else if (msg.is_success === 1){
            console.log("success");

            // 设置变量
            this.obj_DataSet.data_set_list.list_space[this.obj_DataSet.current_active_data_set].target_xpath = msg.xpath;
            this.obj_DataSet.data_set_list.list_space[this.obj_DataSet.current_active_data_set].scroll_top = msg.scroll_top;

            // 改变元素表现
            $("#xpath").val(msg.xpath);

            // 检查该数据组是否开启了预览
            if (this.obj_DataSet.data_set_list.list_space[this.obj_DataSet.current_active_data_set].is_preview === true){
                // 发送预览请求
                console.log("data set is acitve preview, sending the preview request");
            }

        }else{
            console.log("==cb_change_data_set_target_value_xpath==");
            console.log("error");
            console.log("msg:");
            console.log(msg);
            return false;
        }
    }
}

class CallBackBasicInfo extends CallBack{

    constructor(){
        super();
    }


    cb_change_basicInfo_value(basicInfo) {
        console.log("CallBackBasicInfo");
        console.log(basicInfo);
        mc_BasicInfo.cookies = basicInfo.cookies;
        mc_BasicInfo.cookies_length = basicInfo.cookies_length;
        mc_BasicInfo.current_url = basicInfo.url;
        $("#target_host").val(mc_BasicInfo.current_url);
    }
}

class CallBackGetDataStructure extends CallBack{
    constructor(){
        super();

        this.obj_DataBindingSet = mc_GetData.structure.data_binding_set;
        this.obj_WarningConfirm = new WarningConfirmBar();
    }

    cb_refresh_viewer(msg){

    }
}


class CallBackDataBinding extends CallBack{
    constructor(){
        super();
        this.obj_DataBindingSet = null;
        this.update_databinding_obj();
        this.obj_WarningConfirm = new WarningConfirmBar();
    }

    update_databinding_obj(){
        if (mc_BasicInfo.data_acquisition_method === GlobalValue.DATA_METHOD_SIMPLE_LIST){
            this.obj_DataBindingSet = mc_GetData.simple_list.data_binding_set;
        }else if(mc_BasicInfo.data_acquisition_method === GlobalValue.DATA_METHOD_STRUCTURE){
            this.obj_DataBindingSet = mc_GetData.structure.data_binding_set;
        }else{
            console.log("==CallBackDataBinding==");
            console.log("Unknown data method");
            console.log("mc_BasicInfo.data_acquisition_method");
            console.log(mc_BasicInfo.data_acquisition_method);
            this.obj_DataBindingSet = null;
        }
    }

    change_xpath_value(msg) {
        console.log("==change xpath value==")
        console.log(msg);
        $('#xpathBinding').val(msg.xpath);
        $("#maxXpathBinding").val(msg.max_data_binding_xpath);
        $("#span_sub_num").css("display", "");
        $("#span_sub_num").text("(子结构数量:" + msg.list_sub_range_xpath.length + ")");
        this.obj_DataBindingSet.xpath_binding = msg.xpath;
        this.obj_DataBindingSet.max_xpath_binding = msg.max_data_binding_xpath;
        this.obj_DataBindingSet.sub_xpath_binding_list = msg.list_sub_range_xpath
        // this.update_databinding_obj()
    
        // 判断预览按钮是否激活,如果激活,显示xpath元素
        if (this.obj_DataBindingSet.is_preview === true){
            this.obj_DataBindingSet.m_active_preview();
        }
    }
    
    change_ori_xpath_value(msg){
        $('#xpathBinding').val(msg.xpath);
        $("#maxXpathBinding").val(msg.max_data_binding_xpath);
        $("#span_sub_num").css("display", "");
        $("#span_sub_num").text("(子结构数量:" + msg.list_sub_range_xpath.length + ")");
        this.obj_DataBindingSet.xpath_binding = msg.xpath;
        this.obj_DataBindingSet.xpath_binding_ori = msg.xpath;
        this.obj_DataBindingSet.max_xpath_binding = msg.max_data_binding_xpath;
        this.obj_DataBindingSet.sub_xpath_binding_list = msg.list_sub_range_xpath
    
    
        // 判断预览按钮是否激活,如果激活,显示xpath元素
        if (this.obj_DataBindingSet.is_preview === true){
            this.obj_DataBindingSet.m_active_preview();
        }
    }

    /*
    获取所有数据标定位置
    - 数据位置
    - 最大范围(数据位置的父节点)
    - 子结构
    */
    cb_change_xpath_value(msg) {
        console.log("==cb_change_xpath_value==");
        console.log(msg);

        if (msg.is_success === -1){
            // web_info_object.display_warning("超过了锁定的最大范围");
            this.obj_WarningConfirm.display_warning("超过了锁定的最大范围");
        }else{
            this.change_ori_xpath_value(msg);
        }
    }

    /*
        手动获取最大范围
    */
    cb_change_maxRangeXpath_value(msg){
        console.log(msg);

        if (msg.is_success === false){
            // 指定的最大范围无效, 标定的数据位置不在指定的范围之内
            // web_info_object.display_warning("指定的最大范围无效, 标定的数据位置不在指定的范围之内");
            this.obj_WarningConfirm.display_warning("指定的最大范围无效, 标定的数据位置不在指定的范围之内");
        }else if(msg.is_success === true){
            $('#maxXpathBinding').val(msg.xpath);
            this.obj_DataBindingSet.max_xpath_binding = msg.xpath;

            // 检查预览状态
            // if (web_info_object.data_binding_set.is_data_binding_done() === true && web_info_object.data_binding_set.is_preview === true){
            //     // 已打开预览功能
            //     // 发送预览请求
            //     web_info_object.data_binding_set.m_active_preview();
            // }

            // 根据返回的最大xpath,生成子结构
            // 检查数据标定状态, 如果完成,生成子结构
            if (this.obj_DataBindingSet.is_data_binding_done() === true){
                this.obj_DataBindingSet.m_get_sub_with_selected_data();
            }
        }else{
            console.log("==cb_change_maxRangeXpath_value==");
            console.log("error");
            console.log("msg:");
            console.log(msg);
            return false;
        }
    }

    // 调整最大xpath    max_range_xpath
    cb_maxRangeXpath_up(msg){
        if (msg.is_success === -1){
            // 调整失败
            // change_maxRangeXpath_value(msg.max_range_xpath);
            // web_info_object.display_warning("已经到达最大范围");
            this.obj_WarningConfirm.display_warning("已经到达最大范围");
        }else if(msg.is_success === 1){
            // 调整成功
            console.log("调整成功");

            // 改变元素表现
            let element_obj = {
                xpath: msg.xpath,
                max_data_binding_xpath: msg.max_range_xpath,
                list_sub_range_xpath: msg.list_sub_range_xpath
            }
            this.change_xpath_value(element_obj);

            // 检查预览状态
            if (this.obj_DataBindingSet.is_data_binding_done() === true && this.obj_DataBindingSet.is_preview === true){
                // 已打开预览功能
                // 发送预览请求
                this.obj_DataBindingSet.m_active_preview();
            }
        }else{
            console.log("==maxRangeXpath_up==");
            console.log("error");
            console.log("msg:");
            console.log(msg);
            return false;
        }
    }

    cb_maxRangeXpath_down(msg){
        if (msg.is_success === -1){
            // change_maxRangeXpath_value(msg.max_range_xpath);
            // web_info_object.display_warning("已经到达最小范围");
            this.obj_WarningConfirm.display_warning("已经到达最小范围");
        }else if(msg.is_success === 1){
            // 调整成功
            console.log("调整成功");

            // 改变元素表现
            let element_obj = {
                xpath: msg.xpath,
                max_data_binding_xpath: msg.max_range_xpath,
                list_sub_range_xpath: msg.list_sub_range_xpath
            }
            this.change_xpath_value(element_obj);

            // 检查预览状态
            if (this.obj_DataBindingSet.is_data_binding_done() === true && this.obj_DataBindingSet.is_preview === true){
                // 已打开预览功能
                // 发送预览请求
                this.obj_DataBindingSet.m_active_preview();
            }

        }else{
            console.log("==maxRangeXpath_down==");
            console.log("error");
            console.log("msg:");
            console.log(msg);
            return false;
        }
    }

    // 调整data binding 范围
    cb_xpathBinding_up(msg){
        if (msg.is_success === -1){
            // web_info_object.display_warning("已经到达最大范围");
            this.obj_WarningConfirm.display_warning.display_warning("已经到达最大范围");
        }else if (msg.is_success === 1){
            // 调整成功
            console.log("调整成功");

            // 改变元素表现
            let element_obj = {
                xpath: msg.xpath,
                max_data_binding_xpath: msg.max_range_xpath,
                list_sub_range_xpath: msg.list_sub_range_xpath
            }
            this.change_xpath_value(element_obj);

            // 检查预览状态
            if (this.obj_DataBindingSet.is_data_binding_done() === true && this.obj_DataBindingSet.is_preview === true){
                // 已打开预览功能
                // 发送预览请求
                this.obj_DataBindingSet.m_active_preview();
            }
        }else{
            console.log("==xpathBinding_up==");
            console.log("error");
            console.log("msg:");
            console.log(msg);
            return false;
        }
    }

    cb_xpathBinding_down(msg){
        if (msg.is_success === -1){
            // web_info_object.display_warning("已经到达最小范围");
            this.obj_WarningConfirm.display_warning("已经到达最小范围");
        }else if (msg.is_success === 1){
            // 改变元素表现
            let element_obj = {
                xpath: msg.xpath,
                max_data_binding_xpath: msg.max_range_xpath,
                list_sub_range_xpath: msg.list_sub_range_xpath
            }
            this.change_xpath_value(element_obj);

            // 检查预览状态
            if (this.obj_DataBindingSet.is_data_binding_done() === true && this.obj_DataBindingSet.is_preview === true){
                // 已打开预览功能
                // 发送预览请求
                this.obj_DataBindingSet.m_active_preview();
            }

        }else{
            console.log("==xpathBinding_down==");
            console.log("error");
            console.log("msg:");
            console.log(msg);
            return false;
        }
    }

    // 改变翻页按钮
    cb_change_turn_page_button(msg){
        $(GlobalValue.id_txtTurnPageButtonXpath).val(msg.xpath);
        mc_GetData.simple_list.data_binding_set.turn_page_xpath = msg.xpath;
        mc_GetData.simple_list.data_binding_set.turn_page_scrolltop = msg.scrollTop;
    }

    // 
    cb_change_waterfall_btn_xpath(msg){
        $(GlobalValue.id_txtWaterFallButtonXpath).val(msg.xpath);
        mc_GetData.simple_list.data_binding_set.waterfall_btn_xpath = msg.xpath;
        mc_GetData.simple_list.data_binding_set.waterfall_btn_scrolltop = msg.scrollTop;
    }
}

class CallBackHtmlAnalyzer extends CallBack{
    constructor(){
        super();
        this.obj_Analyzer = null;
        this.update_analyzer_obj();
    }

    update_analyzer_obj(){
        this.obj_Analyzer = mc_GetData.structure.html_analyzer;
    }

    cb_change_html_analyzer(msg){
        // 初始化html analyzer
        this.obj_Analyzer.init_analyzer(msg.cross_list_space, msg.cross_node_space);

        // 构造viewer
        // this.obj_Analyzer.obj_ElementsViewer.generate_viewer();
    }

    cb_refresh_viewer(msg){
        this.obj_Analyzer.init_analyzer(msg.cross_list_space, msg.cross_node_space);
    }
}


class CallBackContinues extends CallBack{
    constructor(){
        super();
        this.obj_continues = mc_GetData.continues;
    }

    cb_get_xpath_value(msg){
        this.obj_continues.model.value_xpath = msg.xpath;
        this.obj_continues.refresh();
    }
}

class CallBackSendData extends CallBack{
    constructor(){
        super();
        this.obj_send_data = mc_SendData;
    }

    cb_get_table_data(msg){
        if (mc_GetData.simple_list.data_binding_set.data_refresh_type === GlobalValue.DATA_REFRESH_WATERFALL_BTN){
            this.obj_send_data.fill_waterfall_btn_content(msg.data_list, msg.sub_xpath_list);
        }else if(mc_GetData.simple_list.data_binding_set.data_refresh_type === GlobalValue.DATA_REFRESH_WATERFALL){
            this.obj_send_data.fill_waterfall_content(msg.data_list, msg.sub_xpath_list);
        }else{
            this.obj_send_data.fill_table_content(msg.data_list, msg.sub_xpath_list);
        }
    }

    cb_get_table_data_structure(msg){
        this.obj_send_data.fill_structure_table_content(msg.data_list);
        this.obj_send_data.export_data_to_excel();
    }

    cb_stop_get_data(msg){
        this.obj_send_data.stop_get_data();
    }

}

/*
=======================================
全局变量
=======================================
*/

// 消息类型
const MSG_NORMAL = 0;                           // background.js 不必改动发送的消息, 可以直接发送给content.js
const MSG_SPEFIFIC = 1;                         // background.js 需要重新包装消息


// 工具类
var utility_for_panel = new UtilityForPanel();


// 元素
var mc_Navigation = new Navigation();
var mc_GetData = new GetData();
var mc_BasicInfo = new BasicInfo();
var mc_SendData = new SendData();

// 回调
var mc_CallBackSimpleList = new CallBackSimpleList();
var mc_CallBackBasicInfo = new CallBackBasicInfo();
var mc_CallBackGetDataStructure = new CallBackGetDataStructure();
var mc_CallBackDataBinding = new CallBackDataBinding();
var mc_CallBackHtmlAnalyzer = new CallBackHtmlAnalyzer();
var mc_CallBackContinues = new CallBackContinues();
var mc_callBackSendData = new CallBackSendData();

// 代表项目本身
var web_info_object = new WebInfo();



/*
=======================================
接口回调函数 callback function(cbf)
从devtool.js 接受来自background的信息

函数形式: "cbf_***"
=======================================
*/
function cbf_received_msg_from_background(msg){


    // 更新对象
    mc_CallBackDataBinding.update_databinding_obj();
    mc_CallBackHtmlAnalyzer.update_analyzer_obj();

    // 来自databinding的回调
    if(msg.todo == "data_binding_xpath"){
        if (msg.todo_type === GlobalValue.XPATH_TYPE_SIMPLIST_TURN_PAGE_BUTTON){
            mc_CallBackDataBinding.cb_change_turn_page_button(msg);
        }else if(msg.todo_type === GlobalValue.XPATH_TYPE_STRUCTURE_GET_DATA_BINDING){
            // mc_CallBackGetDataStructure.cb_get_data_binding_value(msg);
            // 改变databinding
            mc_CallBackDataBinding.cb_change_xpath_value(msg);

            // 改变html analyzer
            mc_CallBackHtmlAnalyzer.cb_change_html_analyzer(msg);

        }else if(msg.todo_type === GlobalValue.XPATH_TYPE_SIMPLIST_WATERFALL_BUTTON){
            mc_CallBackDataBinding.cb_change_waterfall_btn_xpath(msg);
        }else{
            mc_CallBackDataBinding.cb_change_xpath_value(msg);    
        }
    }else if(msg.todo == "maxRangeXpath"){
        mc_CallBackDataBinding.cb_change_maxRangeXpath_value(msg);
        // mc_CallBackSimpleList.cb_change_maxRangeXpath_value(msg);   
    }else if(msg.todo == "subRangeXpath"){
        mc_CallBackDataBinding.cb_change_subRangeXpath_value(msg);
        // mc_CallBackSimpleList.cb_change_subRangeXpath_value(msg);
    }else if(msg.todo == "adjust_max_xpath_up"){
        mc_CallBackDataBinding.cb_maxRangeXpath_up(msg);
        // mc_CallBackSimpleList.cb_maxRangeXpath_up(msg);
    }else if(msg.todo == "adjust_max_xpath_down"){
        mc_CallBackDataBinding.cb_maxRangeXpath_down(msg);
        // mc_CallBackSimpleList.cb_maxRangeXpath_down(msg);
    }else if(msg.todo == "adjust_binding_xpath_up"){
        mc_CallBackDataBinding.cb_xpathBinding_up(msg);
        // mc_CallBackSimpleList.cb_xpathBinding_up(msg);
    }else if(msg.todo == "adjust_binding_xpath_down"){
        mc_CallBackDataBinding.cb_xpathBinding_down(msg);
        // mc_CallBackSimpleList.cb_xpathBinding_down(msg);
    }else if(msg.todo === "getSubWithSelectedData"){
        mc_CallBackDataBinding.cb_change_xpath_value(msg);
        // mc_CallBackSimpleList.cb_change_xpath_value(msg);
    }else if(msg.todo === "data_set_target_value"){
        mc_CallBackSimpleList.cb_change_data_set_target_value_xpath(msg);
        // mc_CallBackSimpleList.cb_change_data_set_target_value_xpath(msg);
    }else if(msg.todo === "refresh_viewer"){
        mc_CallBackHtmlAnalyzer.cb_refresh_viewer(msg);
    }else if(msg.todo === "get_table_data"){
        mc_callBackSendData.cb_get_table_data(msg);
    }else if(msg.todo === "get_table_data_structure"){
        mc_callBackSendData.cb_get_table_data_structure(msg);
    }else if(msg.todo === "stop_get_data"){
        mc_callBackSendData.cb_stop_get_data(msg);
    }

    // basic
    if(msg.todo == "basicInfo"){
        mc_CallBackBasicInfo.cb_change_basicInfo_value(msg);
    }

    // 来自continues的回调
    if(msg.todo === "getDataContinues"){
        mc_CallBackContinues.cb_get_xpath_value(msg);
    }
}